In [1]:
Copied!
import opendatasets as od
data_path = 'https://www.kaggle.com/datasets/abdallahwagih/kvasir-dataset-for-classification-and-segmentation'
od.download(data_path)
import opendatasets as od data_path = 'https://www.kaggle.com/datasets/abdallahwagih/kvasir-dataset-for-classification-and-segmentation' od.download(data_path)
Skipping, found downloaded files in "./kvasir-dataset-for-classification-and-segmentation" (use force=True to force download)
1. Prepare data¶
In [2]:
Copied!
# Read image and mask paths
image_paths = []
mask_paths = []
import os
images_root = './kvasir-dataset-for-classification-and-segmentation/kvasir-seg/Kvasir-SEG/images'
masks_root = './kvasir-dataset-for-classification-and-segmentation/kvasir-seg/Kvasir-SEG/masks'
for image, mask in zip(os.listdir(images_root), os.listdir(masks_root)):
image_paths.append(os.path.join(images_root, image))
mask_paths.append(os.path.join(masks_root, mask))
# Read image and mask paths image_paths = [] mask_paths = [] import os images_root = './kvasir-dataset-for-classification-and-segmentation/kvasir-seg/Kvasir-SEG/images' masks_root = './kvasir-dataset-for-classification-and-segmentation/kvasir-seg/Kvasir-SEG/masks' for image, mask in zip(os.listdir(images_root), os.listdir(masks_root)): image_paths.append(os.path.join(images_root, image)) mask_paths.append(os.path.join(masks_root, mask))
Try converting images and masks to their true channel and check number of channels of images and masks¶
In [3]:
Copied!
from PIL import Image
print('Image: ', Image.open(image_paths[0]).convert('RGB').getbands())
print('Mask: ', Image.open(mask_paths[0]).convert('L').getbands())
from PIL import Image print('Image: ', Image.open(image_paths[0]).convert('RGB').getbands()) print('Mask: ', Image.open(mask_paths[0]).convert('L').getbands())
Image: ('R', 'G', 'B')
Mask: ('L',)
Check the shape of images and masks¶
In [4]:
Copied!
import numpy as np
print('Images: ', np.array(Image.open(image_paths[0]).convert('RGB')).shape)
print('Masks: ', np.array(Image.open(mask_paths[0]).convert('L')).shape)
import numpy as np print('Images: ', np.array(Image.open(image_paths[0]).convert('RGB')).shape) print('Masks: ', np.array(Image.open(mask_paths[0]).convert('L')).shape)
Images: (529, 622, 3) Masks: (529, 622)
In [5]:
Copied!
import matplotlib.pyplot as plt
def Visualize_Data():
fig, axes = plt.subplots(10, 7, figsize=(28, 40))
for i in range(0, 10, 2):
for j in range(0, 7):
num = i // 2 * 10 + j
image = Image.open(image_paths[num])
image = image.resize((256, 256))
mask = Image.open(mask_paths[num])
mask = mask.resize((256, 256))
axes[i, j].imshow(image)
axes[i, j].axis('off')
axes[i + 1, j].imshow(mask)
axes[i + 1, j].axis('off')
plt.subplots_adjust(wspace=0.01, hspace=0.01)
plt.show()
Visualize_Data()
import matplotlib.pyplot as plt def Visualize_Data(): fig, axes = plt.subplots(10, 7, figsize=(28, 40)) for i in range(0, 10, 2): for j in range(0, 7): num = i // 2 * 10 + j image = Image.open(image_paths[num]) image = image.resize((256, 256)) mask = Image.open(mask_paths[num]) mask = mask.resize((256, 256)) axes[i, j].imshow(image) axes[i, j].axis('off') axes[i + 1, j].imshow(mask) axes[i + 1, j].axis('off') plt.subplots_adjust(wspace=0.01, hspace=0.01) plt.show() Visualize_Data()